using System.Collections.Generic;

namespace ServiceModel.Syndication.LEAP2A {
    /// <summary>
    /// Manages pairs of LEAP2A relationship types
    /// </summary>
    public class LeapLinkManager {
        private readonly Dictionary<LeapPredicates, LeapLinkPair> lookup;
        private readonly Dictionary<string, LeapLinkPair> customLookup;

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapLinkManager"/> class.
        /// </summary>
        public LeapLinkManager() {
            lookup = new Dictionary<LeapPredicates, LeapLinkPair>();
            customLookup = new Dictionary<string, LeapLinkPair>();

            // Add standard link types;
            AddLinkPair(LeapLinkPair.Attendee);
            AddLinkPair(LeapLinkPair.Evidence);
            AddLinkPair(LeapLinkPair.Outcome);
            AddLinkPair(LeapLinkPair.Part);
            AddLinkPair(LeapLinkPair.Reflection);
            AddLinkPair(LeapLinkPair.Related);
            AddLinkPair(LeapLinkPair.Relation);
            AddLinkPair(LeapLinkPair.Support);
            AddLinkPair(LeapLinkPair.Agenda);
        }

        /// <summary>
        /// Adds a pair of matching links.
        /// </summary>
        /// <param name="linkPair">The link pair.</param>
        public void AddLinkPair(LeapLinkPair linkPair) {
            LeapLinkPair inverse = linkPair.Invert();

            if (linkPair.Value == LeapPredicates.Custom) {
                customLookup[linkPair.CustomValue] = linkPair;
                customLookup.Add(linkPair.CustomInverse, inverse);
            } else {
                lookup[linkPair.Value] = linkPair;
                lookup.Add(linkPair.Inverse, inverse);
            }
        }

        /// <summary>
        /// Looks up the inversve of a relationship.
        /// </summary>
        /// <param name="linkType">Type of the link.</param>
        /// <returns></returns>
        public LeapLinkPair LookupLinkPair(LeapPredicates linkType) {
            if (lookup.ContainsKey(linkType)) {
                return lookup[linkType];
            }

            return null;
        }

        public LeapLinkPair LookupLinkPair(string linkType) {
            if (customLookup.ContainsKey(linkType)) {
                return customLookup[linkType];
            }

            return null;
        }
    }
}
